package com.java.simple.study.juc.p5;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoujunlin
 * @date 2022年06月30日 21:05
 * @desc
 */
public interface DecimalAccount {
    /**
     * 获取余额
     *
     * @return
     */
    BigDecimal getBalance();

    /**
     * 取款
     *
     * @param amount
     */
    void withdraw(BigDecimal amount);

    static void demo(DecimalAccount account) {
        List<Thread> ts = new ArrayList<>();
        long start = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            ts.add(new Thread(() -> {
                account.withdraw(BigDecimal.TEN);
            }));
        }
        ts.forEach(Thread::start);
        ts.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long end = System.nanoTime();
        System.out.println("剩余:" + account.getBalance() + " cost:" +
                (end - start) / 1000_000 + " ms");
    }


}
